//
//  NBARegisterViewController.m
//  NBAirCleaner
//
//  Created by 白浩洋 on 2018/2/23.
//  Copyright © 2018年 HaoYang. All rights reserved.
//

#import "NBARegisterViewController.h"
@interface NBARegisterViewController ()
@property (nonatomic,strong)UITextField * phoneFiled;
@property (nonatomic,strong)UITextField * yanzhengFiled;
@property (nonatomic,strong)UITextField * psdFiled;
@property (nonatomic,strong)UIButton * registerBtn;
@property (nonatomic,strong)UIButton * miBtn;
@property (nonatomic,strong)UIButton * sendCodeButton; // 发送验证码
@end

@implementation NBARegisterViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if(self.type == 1){
        [self addCustomTitleWithTitle:@"新用户注册"];
    }else{
        [self addCustomTitleWithTitle:@"忘记密码"];
    }
    [self addCustomBackBarButtonItemWithTarget:self action:@selector(backUp)];
    [self setUpUI];
}
- (void)setUpUI{
    self.phoneFiled.center = CGPointMake(ScreenWidth/2.0f, PXChange(121) +self.phoneFiled.height/2.0f);
    UILabel *lineOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), 1)];
    lineOne.backgroundColor = [UIColor colorWithHexString:@"#EDEDF0"];
    lineOne.center = CGPointMake(ScreenWidth/2.0f, self.phoneFiled.bottom);
    [self.view addSubview:lineOne];
    self.yanzhengFiled.center = CGPointMake(PXChange(74) + self.yanzhengFiled.width/2.0f, self.phoneFiled.bottom + PXChange(36) +self.yanzhengFiled.height/2.0f);
    UILabel *lineTwo = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), 1)];
    lineTwo.backgroundColor = [UIColor colorWithHexString:@"#EDEDF0"];
    lineTwo.center = CGPointMake(ScreenWidth/2.0f, self.yanzhengFiled.bottom);
    [self.view addSubview:lineTwo];
    self.psdFiled.center = CGPointMake(ScreenWidth/2.0f, PXChange(60) + lineTwo.bottom + self.psdFiled.height/2.0f);
    UILabel *lineThree = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), 1)];
    lineThree.backgroundColor = [UIColor colorWithHexString:@"#EDEDF0"];
    lineThree.center = CGPointMake(ScreenWidth/2.0f, self.psdFiled.bottom);
    [self.view addSubview:lineThree];
    self.registerBtn.center = CGPointMake(ScreenWidth/2.0f, PXChange(60) + lineThree.bottom + self.registerBtn.height/2.0f);
    self.sendCodeButton.center = CGPointMake(ScreenWidth - PXChange(74) - self.sendCodeButton.width/2.0f, self.yanzhengFiled.centerY);
    self.miBtn.center = CGPointMake(ScreenWidth - PXChange(166), self.psdFiled.centerY);
    self.yanzhengFiled.center = CGPointMake(ScreenWidth/2.0f, self.phoneFiled.bottom + PXChange(36) +self.yanzhengFiled.height/2.0f);
    self.psdFiled.center = CGPointMake(ScreenWidth/2.0f, PXChange(60) + lineTwo.bottom + self.psdFiled.height/2.0f);
    self.yanzhengFiled.left = self.phoneFiled.left;
    if(self.type != 1){
        lineThree.hidden = YES;
        self.yanzhengFiled.hidden = YES;
    }
    
}
-(void)textFieldDidChange :(UITextField *)theTextField{
    if(self.phoneFiled.text.length >0 && self.psdFiled.text.length >0 && self.yanzhengFiled.text.length >0){
        [self.registerBtn setEnabled:YES];
        [self.registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.registerBtn setBackgroundColor:[UIColor colorWithHexString:BH_Color_Main]];
    }else{
        [self.registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.registerBtn setBackgroundColor:[UIColor lightGrayColor]];
    }
    if([NBATools isMobileNumber:self.phoneFiled.text]){
        self.sendCodeButton.layer.borderColor = [UIColor colorWithHexString:BH_Color_Main].CGColor;
        [self.sendCodeButton setTitleColor:[UIColor colorWithHexString:BH_Color_Main] forState:UIControlStateNormal];
        [self.sendCodeButton setEnabled:YES];
    }else{
        self.sendCodeButton.layer.borderColor = [UIColor colorWithHexString:@"#C7C7CD"].CGColor;
        [self.sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#C7C7CD"] forState:UIControlStateNormal];
        [self.sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.sendCodeButton setEnabled:NO];
    }
}
- (void)codeClick{
    [self.sendCodeButton startWithTime:60 title:@"获取验证码" countDownTitle:@"S" AmainColor:[UIColor colorWithHexString:BH_Color_Main] countColor:[UIColor colorWithHexString:BH_Color_Main] WithFiled:self.phoneFiled];
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:self.phoneFiled.text andTemplate:@"register" resultBlock:^(int msgId, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            //获得smsID
            NSLog(@"sms ID：%d",msgId);
        }
    }];
}
- (void)registerClick{
    if(self.psdFiled.text.length < 6){
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [SVProgressHUD showInfoWithStatus:@"密码不能小于6位"];
        [SVProgressHUD dismissWithDelay:0.6];
        return;
    }
    [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:self.phoneFiled.text andSMSCode:self.yanzhengFiled.text resultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [self checkUserExits];
        } else {
            
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD showInfoWithStatus:@"请输入正确的验证码"];
            [SVProgressHUD dismissWithDelay:0.6];
        }
    }];
}
- (void)checkUserExits{
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:self.phoneFiled.text];
    [bUser setPassword:self.psdFiled.text];
    [bUser setObject:self.psdFiled.text forKey:@"psd"];
    [bUser setObject:self.yanzhengFiled.text forKey:@"verCode"];
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            BmobUser *cuser = [BmobUser currentUser];
            //在GameScore创建一条数据，如果当前没GameScore表，则会创建GameScore表
            BmobObject  *gameScore = [BmobObject objectWithClassName:@"star"];
            //score为1200
            [gameScore setObject:cuser.username forKey:@"userid"];
            //设置userName为小明
            if([[NBATools getDeviceIDInKeychain] isEqualToString:@"first"]){
                [gameScore setObject:[NSNumber numberWithInteger:300] forKey:@"star"]; //首次注册送300星
            }else{
                [gameScore setObject:[NSNumber numberWithInteger:0] forKey:@"star"];
            }
            [gameScore setObject:[NBATools getDeviceIDInKeychain] forKey:@"udid"];
            [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                NSString *showStr = @"";
                if (isSuccessful) {
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                    [SVProgressHUD dismissWithDelay:0.6];
                    [self backUp];
                } else if (error){
                    if(error.code == 202){
                        showStr = @"账号已被注册";
                    }else{
                        showStr = @"未知错误";
                    }
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD showInfoWithStatus:showStr];
                    [SVProgressHUD dismissWithDelay:0.6];
                } else {
                    if(error.code == 202){
                        showStr = @"账号已被注册";
                    }else{
                        showStr = @"未知错误";
                    }
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD showInfoWithStatus:showStr];
                    [SVProgressHUD dismissWithDelay:0.6];
                }
            }];
        } else {
            NSString *showStr = @"";
            if(error.code == 202){
                showStr = @"账号已被注册";
            }else{
                showStr = @"未知错误";
            }
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD showInfoWithStatus:showStr];
            [SVProgressHUD dismissWithDelay:0.6];
        }
    }];
}
- (void)miClick{
    self.miBtn.selected = !self.miBtn.selected;
    if (self.miBtn.selected) { // 按下去了就是明文
        NSString *tempPwdStr = self.psdFiled.text;
        self.psdFiled.text = @""; // 这句代码可以防止切换的时候光标偏移
        self.psdFiled.secureTextEntry = NO;
        self.psdFiled.text = tempPwdStr;
        
    } else { // 暗文
        NSString *tempPwdStr = self.psdFiled.text;
        self.psdFiled.text = @"";
        self.psdFiled.secureTextEntry = YES;
        self.psdFiled.text = tempPwdStr;
    }
}
- (UITextField *)phoneFiled{
    if(!_phoneFiled){
        _phoneFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), PXChange(104))];
        NSString *holderText = @"输入手机号";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#C7C7CD"] range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:PXChange(36)] range:NSMakeRange(0, holderText.length)];
        _phoneFiled.attributedPlaceholder = placeholder;
        _phoneFiled.borderStyle = UITextBorderStyleNone;
        _phoneFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        if (@available(iOS 10.0, *)) {
            _phoneFiled.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
        } else {
            // Fallback on earlier versions
        }
        [_phoneFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_phoneFiled];
    }
    return _phoneFiled;
}
- (UITextField *)yanzhengFiled{
    if(!_yanzhengFiled){
        _yanzhengFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(324), PXChange(104))];
        NSString *holderText = @"输入验证码";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#C7C7CD"] range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:PXChange(36)] range:NSMakeRange(0, holderText.length)];
        _yanzhengFiled.attributedPlaceholder = placeholder;
        _yanzhengFiled.borderStyle = UITextBorderStyleNone;
        _yanzhengFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        if (@available(iOS 10.0, *)) {
            _yanzhengFiled.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
        } else {
            // Fallback on earlier versions
        }
        [_yanzhengFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_yanzhengFiled];
    }
    return _yanzhengFiled;
}
- (UITextField *)psdFiled{
    if(!_psdFiled){
        _psdFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), PXChange(104))];
        NSString *holderText = @"设置密码";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#C7C7CD"] range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:PXChange(36)] range:NSMakeRange(0, holderText.length)];
        _psdFiled.attributedPlaceholder = placeholder;
        _psdFiled.borderStyle = UITextBorderStyleNone;
        _psdFiled.secureTextEntry = YES;
        _psdFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        if (@available(iOS 10.0, *)) {
            _psdFiled.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
        } else {
            // Fallback on earlier versions
        }
        [_psdFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_psdFiled];
    }
    return _psdFiled;
}
- (UIButton *)registerBtn{
    if(!_registerBtn){
        _registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(600), PXChange(100))];
        [_registerBtn setBackgroundColor:[UIColor lightGrayColor]];
        [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registerBtn.titleLabel setFont:[UIFont systemFontOfSize:PXChange(36)]];
        [_registerBtn setTitle:@"完成" forState:UIControlStateNormal];
        _registerBtn.layer.cornerRadius = PXChange(50);
        _registerBtn.clipsToBounds = YES;
        [_registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
        [_registerBtn setEnabled:NO];
        
        [self.view addSubview:_registerBtn];
    }
    return _registerBtn;
}
- (UIButton *)sendCodeButton{
    if(!_sendCodeButton){
        _sendCodeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(180), PXChange(60))];
        [_sendCodeButton setBackgroundColor:[UIColor clearColor]];
        [_sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#C7C7CD"] forState:UIControlStateNormal];
        [_sendCodeButton.titleLabel setFont:[UIFont systemFontOfSize:PXChange(26)]];
        [_sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _sendCodeButton.clipsToBounds = YES;
        _sendCodeButton.layer.cornerRadius = _sendCodeButton.height/2.0f;
        _sendCodeButton.layer.borderColor = [UIColor colorWithHexString:@"#C7C7CD"].CGColor;
        _sendCodeButton.layer.borderWidth = 1;
        [_sendCodeButton addTarget:self action:@selector(codeClick) forControlEvents:UIControlEventTouchUpInside];
        [_sendCodeButton setEnabled:NO];
        [_sendCodeButton setHidden:NO];
        [self.view addSubview:_sendCodeButton];
    }
    return _sendCodeButton;
}
- (UIButton *)miBtn{
    if(!_miBtn){
        _miBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(70), PXChange(44))];
        [_miBtn setImage:[UIImage imageNamed:@"login_mi"] forState:UIControlStateNormal];
        [_miBtn setImage:[UIImage imageNamed:@"login_ming"] forState:UIControlStateSelected];
        [_miBtn addTarget:self action:@selector(miClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_miBtn];
    }
    return _miBtn;
}
@end
